package controllers

import (
	"errors"
	"github.com/gin-gonic/gin"
	"purchase-mall/models"
)

const CtxUserIDKey = "userID"

var ErrorUserNotLogin = errors.New("用户未登录")

// getCurrentUserID 获取当前登录的用户ID
func getCurrentUserID(c *gin.Context) (userID int64, err error) {
	uid, ok := c.Get(CtxUserIDKey)
	if !ok {
		err = ErrorUserNotLogin
		return
	}
	userID, ok = uid.(int64)
	if !ok {
		err = ErrorUserNotLogin
		return
	}
	return
}

func getPramsInfo(c *gin.Context) (page, size int64, filters map[string]interface{}) {
	//pageStr := c.Query("pageNum")
	//sizeStr := c.Query("pageSize")
	var json models.ParamPage
	// 将request的body中的数据，自动按照json格式解析到结构体
	if err := c.ShouldBindJSON(&json); err != nil {
		// 返回错误信息
		// gin.H封装了生成json数据的工具
		//c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		//return
		page = 1
		size = 10
	}
	page = json.PageNum
	size = json.PageSize
	filters = json.Filters
	//fmt.Println(json)

	//page, err = strconv.ParseInt(pageStr, 10, 64)
	//if err != nil {
	//	page = 1
	//}
	//size, err = strconv.ParseInt(sizeStr, 10, 64)
	//if err != nil {
	//	size = 10
	//}
	return
}
